<?php
class returpenjualancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('masterbank_model');
		$this->load->model('transaksibankpembelian_model');
		$this->load->model('kwitansimutasicash_model');
		$this->load->model('kwitansimutasibank_model');
		$this->load->model('kwitansimutasigiro_model');
		$this->load->model('kwitansipenjualan_model');
		$this->load->model('fakturproductpenjualan_model');
		$this->load->model('returpenjualan_model');
		$this->load->model('stock_model');
		$this->load->model('mstraccount_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{    		           
		if (isset($_POST['btnview'])) 
		{
            if (isset($_POST['fakturkode'])) 
			{
				$nofaktur = $_POST['fakturkode'];
				$this->kwitansipenjualan_model->setNoFaktur($nofaktur);
				$dataprofile = array(
					'page_title' => 'Sales > Retur Penjualan',
					'name' => $this->session->userdata('fullname'),
					'transreturjual' => $this->fakturproductpenjualan_model->getlistnofakturlike(),
					'message'=>''
				);  
            }
        } 
		else 
		{			
			$dataprofile = array(
				'page_title' => 'Sales > Retur Penjualan',
				'name' => $this->session->userdata('fullname'),
				'transreturjual' => $this->fakturproductpenjualan_model->getlistnofaktur(),
				'message'=>''
			);		
		}
		$content = array(
			'content' => 'sales/returpenjualan/inputreturpenjualan'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function simpanretur() 
	{
        if (isset($_POST['btnsave'])) 
		{
			//var_dump($_POST);exit;
			if (isset($_POST['keterangan']) && $_POST['keterangan'] !="" 
				/*or isset($_POST['keterangangiro']) && $_POST['keterangangiro'] !=""*/)
			{
				$jumdata = count($this->input->post('chkitem'));
				//var_dump($this->input->post('chkitem'));exit;
				$i = 0;
				$ttlamtin=0;
				$ttlqty="";
				$stockstatus="RETUR PENJUALAN";
				$ttlamt="";
				$ttlfakqty = 0;
				$Qtyall = 0;
				$tglfaktur =date('Y-m-d', strtotime('now'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));

				$kodenoretur = $this->returpenjualan_model->getReturnNoCode();
				$this->returpenjualan_model->setReturnNo($kodenoretur);
				
				foreach($this->input->post('chkitem') as $iddelay)
				{			
					$iddetailretur = $this->returpenjualan_model->getdetailid();
				
					$kddtlbarang = $_POST['chkitem'][$i];
					$kdfakno	 = $_POST['txtNofaktur'][$i];
					//$kdiddetail	 = $_POST['kdedetailid'][$i];
					$namacust	 = $_POST['txtcustomer'][$i];
					$hrgbarang	 = $_POST['txthrgbrg'][$i];
					$quanttity	 = $_POST['txtQuantity'][$i];
					$disct		 = 0;
					$amount		 = $_POST['txtAmount'][$i];
					$ttlqty		 = $ttlqty + $quanttity;
					$ttlamt		 = $ttlamt + $amount;
					$suppcode 	 = $_POST['kdsupp'][$i];
					
					
					// insert ke tabel transaksi return_product_penjualan			
					$this->returpenjualan_model->setReturnNo($kodenoretur);
					$this->returpenjualan_model->setIDdetail($iddetailretur);
					$this->returpenjualan_model->setKodeDetailBarang($kddtlbarang);
					$this->returpenjualan_model->setHargaBarang($hrgbarang);
					$this->returpenjualan_model->setQTY($quanttity);
					$this->returpenjualan_model->setDiscount($disct);
					$this->returpenjualan_model->setAmount($amount);
					$this->returpenjualan_model->setNoFaktur($kdfakno);
					
					$this->returpenjualan_model->insertdetail();
					
					// insert ke tabel stock dan transaksi stock
					$this->stock_model->setKodeSupplier($suppcode);
					$this->stock_model->setKodeDetailBarang($kddtlbarang);
					$stockArray['stok'] = $this->stock_model->cekkodestck();
					$stockkode = $this->stock_model->getStockListId();
					$stockAkhir = $this->stock_model->getStockCurrent();

					if($stockArray['stok'] != "0")
					{
						
						$this->stock_model->setKodeStok($stockkode[0]->kodestok);
						$latest_stock = intval($stockAkhir[0]->stockakhir) + intval($quanttity);
						$this->stock_model->setStokAkhir($latest_stock);
						$this->stock_model->setLastUpdateBy($this->session->userdata('userid'));
						$this->stock_model->setLastUpdateOn(date('Y-m-d'));
						$this->stock_model->update();
						
						$this->stock_model->setKodeStok($stockkode[0]->kodestok);
						$this->stock_model->setQuantity($quanttity);
						$this->stock_model->setStatus($stockstatus);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insertdetail();
					}
					else
					{
				
						$codestock = $this->stock_model->ambilkodestock();
						$this->stock_model->setKodeStok($codestock);
						$this->stock_model->setKodeSupplier($suppcode);
						$this->stock_model->setKodeDetailBarang($kddtlbarang);
						$this->stock_model->setStokAkhir($quanttity);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insert();	
						
						$this->stock_model->setKodeStok($codestock);
						$this->stock_model->setQuantity($quanttity);
						$this->stock_model->setStatus($stockstatus);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insertdetail();
					}
					$this->fakturproductpenjualan_model->setNoFaktur($kdfakno);
					$deletesession = $this->fakturproductpenjualan_model->deleteFakturProductPenjualan();
					
					$i++;
				}
				
				// insert ke tabel return_product_penjualan			
				$this->returpenjualan_model->setReturnNo($kodenoretur);
				$this->returpenjualan_model->setNamacustomer($namacust);
				$this->returpenjualan_model->setDateReturn($tglfaktur);
				$this->returpenjualan_model->setQuantity($quanttity);
				$this->returpenjualan_model->setPrice($disct);
				$this->returpenjualan_model->setKeterangan($amount);
				$this->returpenjualan_model->setCreatedBy($this->session->userdata('userid'));
				$this->returpenjualan_model->setCreatedOn($tanggal);
				
				$this->returpenjualan_model->insert();
					
				/*
				if ($_POST['typebyr']=="CASH")
				{	
					// insert ke tabel Cash
					$this->kwitansimutasicash_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasicash_model->setNoFaktur("-");
					$this->kwitansimutasicash_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasicash_model->setDiscount("0");
					$this->kwitansimutasicash_model->setTotalAmount_In($ttlamtin);
					$this->kwitansimutasicash_model->setTotalAmount_Out($_POST['ttlamount']);
					$this->kwitansimutasicash_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasicash_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasicash_model->setCreatedOn($tanggal);
					$this->kwitansimutasicash_model->setActive('1');
					
					$this->kwitansimutasicash_model->insert();
				}
				elseif ($_POST['typebyr']=="BANK")
				{
					// insert ke tabel mutasi bank
					$this->kwitansimutasibank_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasibank_model->setNoFaktur("-");
					$this->kwitansimutasibank_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasibank_model->setDiscount("0");
					$this->kwitansimutasibank_model->setTotalAmount_In($ttlamtin);
					$this->kwitansimutasibank_model->setTotalAmount_Out($_POST['ttlamountbank']);
					$this->kwitansimutasibank_model->setKodeBank($_POST['bank']);
					$this->kwitansimutasibank_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasibank_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasibank_model->setCreatedOn($tanggal);
					$this->kwitansimutasibank_model->setActive('1');
					
					$this->kwitansimutasibank_model->insert();
				}
				elseif ($_POST['typebyr']=="GIRO")
				{
					// insert ke tabel mutasi Giro
					
					$this->kwitansimutasigiro_model->setNoKwitansi($kodenokwin);
					$this->kwitansimutasigiro_model->setNoFaktur("-");
					$this->kwitansimutasigiro_model->setTotalQuantity($ttlqty);
					$this->kwitansimutasigiro_model->setTotalAmount_In($ttlamtin);
					$this->kwitansimutasigiro_model->setTotalAmount_Out($_POST['ttlamountgiro']);
					$this->kwitansimutasigiro_model->setBankname($_POST['nmbankgiro']);
					$this->kwitansimutasigiro_model->setNogiro($_POST['nogiro']);
					$this->kwitansimutasigiro_model->setTanggalterbit($_POST['tglterbit']);
					$this->kwitansimutasigiro_model->setTanggalcair($_POST['tglcair']);
					$this->kwitansimutasigiro_model->setKeterangan($_POST['keterangan']);
					$this->kwitansimutasigiro_model->setCreatedBy($this->session->userdata('userid'));
					$this->kwitansimutasigiro_model->setCreatedOn($tanggal);
					$this->kwitansimutasigiro_model->setActive('1');
					
					$this->kwitansimutasigiro_model->insert();
				}
				
										
				// insert ke tabel kwitansi
				$this->kwitansipenjualan_model->setTotalQuantity($ttlqty);
				$this->kwitansipenjualan_model->setTotalAmount($ttlamt);
				$this->kwitansipenjualan_model->setTypebayar($_POST['typebyr']);
				if ($_POST['typebyr']=="CASH")
				{
					$this->kwitansipenjualan_model->setPay_amount($_POST['ttlamount']);
					$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
				}
				elseif ($_POST['typebyr']=="BANK")
				{
					// update ke tabel Account bank
					$this->mstraccount_model->setKodeBank($_POST['bank']);
					
					$stockArray = $this->mstraccount_model->gettotalamount();
					
					$saldoawl = $stockArray[0]->SaldoAwal;
					$uangmsk = $stockArray[0]->Uangmasuk;
					$uangout = $stockArray[0]->Uangkeluar;
					$sldakhr = $stockArray[0]->SaldoAkhir;
					
					$ttlsldawal = $saldoawl;
					$ttluangmsk = $uangmsk;
					$ttluangout = $uangout + $_POST['ttlamountbank'];
					$ttlsldakhr = $sldakhr - $_POST['ttlamountbank'];
					
					$this->mstraccount_model->setSaldoAwal($ttlsldawal);
					$this->mstraccount_model->setUangmasuk($ttluangmsk);
					$this->mstraccount_model->setUangkeluar($ttluangout);
					$this->mstraccount_model->setSaldoAkhir($ttlsldakhr);
					$this->mstraccount_model->setLastupdateby($this->session->userdata('userid'));
					$this->mstraccount_model->setLastUpdateOn($tanggal);
					
					$this->mstraccount_model->ubahmstraccount();
					// sampe sini
					
					$this->kwitansipenjualan_model->setPay_amount($_POST['ttlamountbank']);
					
					$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
				}
				elseif ($_POST['typebyr']=="GIRO")
				{
					$this->kwitansipenjualan_model->setPay_amount($_POST['ttlamountgiro']);
					$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
				}

				$this->kwitansipenjualan_model->setCreatedBy($this->session->userdata('userid'));
				$this->kwitansipenjualan_model->setCreatedOn($tanggal);
				$this->kwitansipenjualan_model->setLastUpdateBy($this->session->userdata('userid'));
				$this->kwitansipenjualan_model->setLastUpdateOn($tanggal);
				$this->kwitansipenjualan_model->setActive('1');
				$this->kwitansipenjualan_model->insert();				
				*/
				
				$dataprofile = array(
					'page_title' => 'Sales > Retur Penjualan',
					'name' => $this->session->userdata('fullname'),
					'koderetur'=> $this->returpenjualan_model->ambilnoretur(),
					'kodecustretur'=> $this->returpenjualan_model->ambilnoretur(),
					'transretur' => $this->returpenjualan_model->getreturno()	
				);		
				$content = array(
					'content' => 'sales/returpenjualan/formreturpenjualan'
				);
				$this->template->load('templates/default',$content,$dataprofile);
				
				//redirect('kwitansipembeliancontroller');
			}
			else
			{
				$dataprofile = array(
					'page_title' => 'Sales > Retur Penjualan',
					'name' => $this->session->userdata('fullname'),
					//'mstrfaktur' => $this->kwitansipenjualan_model->ambilnofaktur(),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Keterangan Belum di isi </div>',
					'transreturjual' => $this->fakturproductpenjualan_model->getlistnofaktur()
                );
                $content = array(
                    'content' => 'sales/returpenjualan/inputreturpenjualan'
                );
                $this->template->load('templates/default', $content, $dataprofile);
			}

        } 
		else 
		{
            $dataprofile = array(
				'page_title' => 'Finance > kwitansi Pembelian',
				'name' => $this->session->userdata('fullname'),
				'transreturjual' => $this->fakturproductpenjualan_model->getlistnofaktur(),
				'message'=>''	
			);	
			
			$content = array
			(
				'content' => 'sales/returpenjualan/inputreturpenjualan'
			);
			$this->template->load('templates/default',$content,$dataprofile);		
		}
    }
}
